﻿//05.Write a program that reads a text file containing a square matrix of numbers and finds in the matrix an area of size 2 x 2 with a maximal sum of its elements. The first line in the input file contains the size of matrix N. Each of the next N lines contain N numbers separated by space. The output should be a single number in a separate text file.

using System;
using System.IO;

namespace Matrix
{
    class Matrix
    {
        static void Main()
        {
            StreamReader reader = new StreamReader("matrix.txt");
            StreamWriter writer = new StreamWriter("MaxResult.txt", false);

            using (reader)
            {
                using (writer)
                {
                    int line = int.Parse(reader.ReadLine());
                    string eachRow = reader.ReadLine();
                    int[,] number = new int[line, line];
                    int row = 0;
                    Console.WriteLine("The matrix in this file is:\n");

                    //Create matrix with size lineXline
                    while (eachRow != null)
                    {
                        string[] numbers = eachRow.Split(' ');
                        for (int col = 0; col < numbers.Length; col++)
                        {
                            number[row, col] = int.Parse(numbers[col]);
                            Console.Write(number[row, col]);
                        }
                        Console.WriteLine();
                        eachRow = reader.ReadLine();
                        row++;
                    }
                    Console.WriteLine();
                    writer.WriteLine(BiggestSum(number, line));
                }
            }
        }

        
        static int BiggestSum(int[,] number, int line)
        {
        //Find the biggest sum
        int curSum = 0;
        int maxSum = 0;

        for (int i = 0; i < line - 1; i++)
        {
            for (int j = 0; j < line - 1; j++)
            {
                curSum = number[i, j] + number[i + 1, j] + number[i, j + 1] + number[i + 1, j + 1];
                if (curSum >= maxSum)
                {
                    maxSum = curSum;
                }
            }
        }

        Console.WriteLine("The max sum of 2x2 matrix is {0}.", maxSum);
        return maxSum;
        }
    }
}
